﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AG.Data
{
    public class AGUnitStore
    {
        public void Save(string publishPath, List<AGUnitCategory> unitCategoryList, List<AGUnit> unitList)
        {
            string unitFile = string.Format("{0}unit.xml", publishPath);

            XDocument xDoc = new XDocument();
            XElement xUnitList = new XElement("unit-list");
            xDoc.Add(xUnitList);

            XElement xCategoryList = new XElement("unit-category-list");
            xUnitList.Add(xCategoryList);
            for (int index = 0; index < unitCategoryList.Count; index++)
            {
                AGUnitCategory category = unitCategoryList[index];
                XElement xCategory = new XElement("unit-category");
                xCategoryList.Add(xCategory);
                xCategory.Add(new XAttribute("id", category.Id));
                xCategory.Add(new XAttribute("caption", category.Caption));
            }

            for (int index = 0; index < unitList.Count; index++)
            {
                AGUnit unit = unitList[index];
                XElement xUnit = new XElement("unit");
                xUnitList.Add(xUnit);

                xUnit.Add(new XAttribute("id", unit.Id));
                xUnit.Add(new XAttribute("category-id", unit.CategoryId));
                xUnit.Add(new XAttribute("caption", unit.Caption));
                xUnit.Add(new XAttribute("model-id", unit.ModelId)); 
            }

            xDoc.Save(unitFile);
        }
    }
}
